//
//  Cordova.m
//  FuncFanc
//
//  Created by careate-hubing on 2021/9/8.
//  Copyright © 2021 huhu. All rights reserved.
//

#import "CordovaViewController.h"
#import "QHHCPManager.h"

@interface CordovaViewController ()
{
    BOOL isNeedReload;
    BOOL isForground;
}
@end
@implementation CordovaViewController

static CordovaViewController * _chooseVCTool = nil;

+ (CordovaViewController *)sharedInstance{
    @synchronized(self)
    {
        if (_chooseVCTool == nil)
        {
            _chooseVCTool = [[CordovaViewController alloc] init];
        }
    }
    return _chooseVCTool;
}

- (void)viewDidLoad {
    __weak typeof(self) ws = self;
    self.wwwFolderContentFolerName = PAT_WWW_FOLDER_NAME;
//    [QHChooseVCTool sharedInstance].wwwContentFolderName = PAT_WWW_FOLDER_NAME;
    self.startPage = @"index.html#/index.html";
//    [QHChooseVCTool sharedInstance].choosedVCPage = @"index.html#/index.html";
    
    //bundle中的www先拷贝过去，重要
    [[QHHCPManager sharedManager] copyBundleToLocal];
//    [self loadURL:self.startPage];
    
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    isNeedReload = NO;
    isForground = YES;
    
    if(![AppDelegate sharedAppDelegate].isInviteUpdating){
        //检查更新
        [[QHHCPManager sharedManager]checkAllBundleUpdateWithMode:@"open" andCompleteBlock:^(BOOL isReload) {
            if (isReload) {
                self->isNeedReload = YES;
                if (self->isForground) {
                    [ws loadURL:ws.startPage];
                }
                else
                {
                }
            }
        }];
    }
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appbecomeactiveNotification:) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ApplicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

-(void)appbecomeactiveNotification:(NSNotification *)notify
{
    isForground = YES;
    if (self->isNeedReload) {
        [self loadURL:self.startPage];
    }
}

-(void)ApplicationDidEnterBackground:(NSNotification *)notify
{
    isForground = NO;
}

- (void)loadURL:(NSString *)url {

    __weak typeof(self) ws = self;
    [[NSOperationQueue mainQueue]addOperationWithBlock:^{
           NSURL *appURL = nil;
           NSURL *startURL = [NSURL URLWithString:ws.startPage];
           NSString *startFilePath = [QHHCPFileManager localStartFileForResource:[startURL path] wwwFolderName:ws.wwwFolderContentFolerName];
           if (startFilePath == nil)
           {
               appURL = nil;
           }
           else
           {
               appURL = [NSURL fileURLWithPath:startFilePath];
               NSString *startPageNoParentDirs = ws.startPage;
               NSRange r = [startPageNoParentDirs rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"?#"] options:0];
               if (r.location != NSNotFound) {
                   NSString *queryAndOrFragment = [ws.startPage substringFromIndex:r.location];
                   appURL = [NSURL URLWithString:queryAndOrFragment relativeToURL:appURL];
               }
           }
           NSURLRequest *request = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];
           [ws.webViewEngine loadRequest:request];
           self->isNeedReload = NO;
    }];
}
-(void)loadView{
    [super loadView];
    self.edgesForExtendedLayout = UIRectEdgeBottom;
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

-(UIUserInterfaceStyle)overrideUserInterfaceStyle{
    return UIUserInterfaceStyleLight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)dealloc{
    
}

@end
